package com.yiyuan.system.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.yiyuan.common.base.BaseDao;
import com.yiyuan.system.dao.IGroupUserDao;
import com.yiyuan.system.model.GroupUser;

@Repository("groupUserDao")
public class GroupUserDaoHbImpl extends BaseDao implements IGroupUserDao {
	public void add(GroupUser groupUser) {
		this.getHibernateTemplate().save(groupUser);
	}

	@SuppressWarnings("unchecked")
	public List<GroupUser> findListByGroupId(Integer groupId) {
		Session session = this.getSession();
		Query query = session
				.createQuery("from GroupUser where groupId=:groupId");
		query.setInteger("groupId", groupId);
		List<GroupUser> list = query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<GroupUser> findDDList(Integer groupId) {
		List guList = new ArrayList();
		GroupUser gu = null;
		Session session = this.getSession();
		Query query = session
				.createSQLQuery("select e.emp_name,u.user_id "
						+ " from yy_sys_employee e,yy_sys_user u,yy_sys_groupuser gu"
						+ " where e.emp_id=u.emp_id and u.user_id=gu.user_id and gu.group_id="
						+ groupId);
		List list = query.list();
		for (int i = 0; i < list.size(); i++) {
			Object users[] = (Object[]) list.get(i);
			gu = new GroupUser();
			gu.setEmpName(users[0].toString());
			gu.setUserId(Integer.valueOf(users[1].toString()));
			guList.add(gu);
		}
		return guList;
	}

	@SuppressWarnings("unchecked")
	public List<GroupUser> findListByUserId(Integer userId) {
		Session session = this.getSession();
		Query query = session
				.createQuery("from GroupUser where userId=:userId");
		query.setInteger("userId", userId);
		List<GroupUser> list = query.list();
		return list;
	}

	public void delete(GroupUser groupUser) {
		this.getHibernateTemplate().delete(groupUser);
	}

}
